package com.kc.sys.controller;


import cn.hutool.Hutool;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.JSONResult;
import com.kc.sys.common.R;
import com.kc.sys.domain.Notice;
import com.kc.sys.domain.User;
import com.kc.sys.service.NoticeService;
import com.kc.sys.util.WebUtils;
import com.kc.sys.vo.LogInfoVo;
import com.kc.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-10-18
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询
     */
    @RequestMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo){
        //获取用户session
        //User user = (User)WebUtils.getSession().getAttribute("user");
        IPage<Notice> page = new Page<>(noticeVo.getPage(),noticeVo.getLimit());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        //ge  >=
        queryWrapper.ge(noticeVo.getStartTime()!=null,"createtime",noticeVo.getStartTime());
        //le  <=
        queryWrapper.le(noticeVo.getEndTime()!=null,"createtime",noticeVo.getEndTime());
        queryWrapper.orderByDesc("createtime");
        noticeService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    //添加公告
    @RequestMapping("addNotice")
    public R addNotice(NoticeVo noticeVo){
        try {
            User user = (User) WebUtils.getSession().getAttribute("user");
            noticeVo.setCreatetime(new Date());
            noticeVo.setOpername(user.getName());
            noticeService.save(noticeVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新公告
    @RequestMapping("updateNotice")
    public R updateNotice(NoticeVo noticeVo){
        try {
            noticeService.updateById(noticeVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteNotice")
    public R deleteById(Integer id){
        try {
            noticeService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    /**
     * 批量删除
     * @param --logInfoVo
     * @return
     */
    @RequestMapping("deleteNotices")
    public R deleteByIds(NoticeVo noticeVo){
        try {
            ArrayList<Serializable> ids = new ArrayList<>();
            for (Integer id : noticeVo.getIds()) {
                ids.add(id);
            }
            noticeService.removeByIds(ids);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    @RequestMapping("uploadFile")
    public DataGridView uploadFile(HttpServletRequest request, @Param("file") MultipartFile file){

        return new DataGridView();
    }
    @RequestMapping("loadNotice")
    public DataGridView loadNotice(NoticeVo noticeVo){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createtime");
        List<Notice> list = noticeService.list(queryWrapper);
        return new DataGridView(list);
    }

    @RequestMapping(value="/toNoticeDocument/{id}", method=RequestMethod.GET)
    public ModelAndView toNoticeDocument(@PathVariable("id") Integer id){
        Notice notice = noticeService.getById(id);
        ModelAndView mv = new ModelAndView("system/notice/notice");
        Date createtime = notice.getCreatetime();
        String format = DateUtil.format(createtime, "yyyy-MM-dd");
        notice.setCreateDate(format);
        mv.addObject("notice",notice);
        return mv;
    }
}

